<?php
require_once('./fun.php');require_once('./hd_fun.php');
require_once ('./daochuExcel.php');
$redis=redis();
$conn=my_sql($cfg);
$id=g('id');
if(!$id or !is_numeric($id)){$id=0;}
$ac=g('ac');
$err['err']='ok';


switch($ac){
case 'adm_login':
$username=g('username');
$pass=g('pass');
if(!$username){err('请输入登录帐号');}
if(!$pass){err('请输入登录密码');}

$sql='select * from '.$cfg['e'].'adm where username=:username and password="'.md5($pass).'" ';
//err($pass);
$p['username']=$username;
$l=db1($sql,$p);
if(!$l){err('抱歉，没有找到用户，或者登录密码错误');}
$arr['last_login']=time();
$arr['session']=md5(time());
update($cfg['e'].'adm',$arr,' where id="'.$l['id'].'"');
$time=time()+3600*24*7;
setcookie($cfg['path'].'_adm_id',$l['id'],$time,'/');
setcookie($cfg['path'].'_adm_session',$arr['session'],$time,'/');
//err('aaa');
err('ok');
break;


case 'loginout':
$time=time()+3600*24*30;
setcookie($cfg['path'].'_adm_id',0,$time,'/');
setcookie($cfg['path'].'_adm_session','',$time,'/');
err('ok');
break;


case 'set_change'://设置修改
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
if($adm['power']!='adm'){err($lan['no_adm_power']);}
$set=get_set();
$data=p('data');if(!$data){err('数据错误');}
$arr=json_decode($data,1);if(!$arr){err('数据错误');}
if($arr['bg']){$arr['bg']=serialize($arr['bg']);}
if($arr['login_bg']){$arr['login_bg']=serialize($arr['login_bg']);}
if($arr['app_bg']){$arr['app_bg']=serialize($arr['app_bg']);}
if($arr['menu']){$arr['menu']=serialize($arr['menu']);}
update($cfg['e'].'set',$arr,' where id=1');
r_set($cfg['path'].'web_set','',300);
err('ok');
break;


case 'set_bg'://设置登录背景图
$re=adm_login([]);
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
if($adm['power']!='adm'){err($lan['no_adm_power']);}
$set=get_set();
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
$arr['img']=$data['img'];
if(!$arr['img']){err('没有选择图片');}
$c=$data['c'];if(!$c){$c='bg';}


if($set[$c]){$bg=$set[$c];
$bg[]=$arr['img'];
}else{
$bg[]=$arr['img'];
}
$arrs[$c]=serialize($bg);
print_r($arr[$c]);
update($cfg['e'].'set',$arrs,' where id=1');
err('ok');

break;



case 'img_up'://图片上传
$re=adm_login();
if($re['err']=='ok'){
$adm=$re['adm'];
$arr['adm_id']=$adm['id'];
}else{//没有登录
$re=user_login();
if($re['err']=='ok'){
$user=$re['user'];
$arr['shop_id']=$re['shop_id'];
$arr['user_id']=$user['id'];
}else{
err('抱歉，您没有登录');
}
}

$arr['name']=p('name');
$arr['ext']=p('ext');
$data=$_POST['data'];
$arr['size']=p('size');

$err['item']=p('item');
//print_r($arr);

$ext=['jpg','png','webp','jpeg'];
if(!in_array($arr['ext'],$ext)){err($arr['name'].'上传文件格式错误');}
$arr['date']=time();
$arr['state']=1;
$id=insert($cfg['e'].'img',$arr);

if(!$id){err('文件上传失败');}
$path='./image/';if(!file_exists($path)){mkdir($path,0700);}
$path='./image/'.ceil($id/1000).'/';
if(!file_exists($path)){mkdir($path,0700);}
$file_name=$id.'.'.$arr['ext'];
if(strstr($data,';base64,')){
$temp=explode(';base64,',$data);
$data=$temp[1];
}

save_file($path,$file_name,base64_decode($data));
resize_img($path,$file_name,$id,$arr['ext'],array(240,480,750,1080));

$err['img']=$id.'|'.$arr['ext'];
$err['img_240']=get_img($err['img'],'_240');
$err['img_480']=get_img($err['img'],'_480');
$err['img_750']=get_img($err['img'],'_750');
$err['img_1080']=get_img($err['img'],'_1080');
$err['id']=$id;

echo json_encode($err);
break;


case 'adm_edit'://管理修改
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
if($adm['power']!='adm'){err($lan['no_adm_power']);}
$data=p('data');if(!$data){err('参数错误');}
$arr=json_decode($data,1);if(!$arr){err('参数错误');}
$id=g('id');
if($id){
unset($arr['power'],$arr['password'],$arr['username']);
if(!$arr){err('数据错误');}
update($cfg['e'].'adm',$arr,' where id="'.$id.'"');err('ok');
}else{
if(!$arr['username']){err('请输入登录名');}
if(!$arr['password']){err('请输入登录密码');}
$arr['password']=md5($arr['password']);
$arr['date']=time();
$arr['power']='user';
$sql='select * from '.$cfg['e'].'adm where  username="'.$arr['username'].'"';
$l=db1($sql);
if($l){err('抱歉，该用户名已经存在');}
insert($cfg['e'].'adm',$arr);
err('ok');
}



break;



case 'html_edit':
$data=p('data');
if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
switch($data['table']){
default:err('编辑权限有误');break;
case 'gg';$re=adm_login(['公告管理']);break;
}


if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
$arr['html']=$data['val'];
if(!$arr['html']){$arr['html']='';}
if(!$id){err('请指定需要编辑的内容');}
$sql='select * from '.$cfg['e'].$data['table'].' where id=:id and comp_id="'.$adm['comp_id'].'"';
$p['id']=$id;
$l=db1($sql,$p);
if(!$l){err('没有找到需要编辑的内容');}
update($cfg['e'].$data['table'],$arr,' where id="'.$l['id'].'"');

err('ok');
break;





case 'get_set':
$set=get_set();
$err['set']=$set;
echo json_encode($err);
break;



case 'get_phone'://获取手机号码
$user=is_login();
$code=g('code');
if(!$code){err('获取手机号码错误');}

$token=getAccessToken ($cfg['wx_xcx_appid'], $cfg['wx_xcx_secret']);
$url='https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$token;
$data['code']=$code;
$re = post( $url, json_encode($data));
if($re['errmsg']=='ok'){
$phone=$re['phone_info']['phoneNumber'];
}else{err('ok');}

if($user){
$arr['phone']=$phone;
update($cfg['e'].'user',$arr,' where id="'.$user['id'].'"');
}
$err['phone']=$phone;
echo json_encode($err);
break;







case 'get_code'://获取登录二维码
$arr['code']=md5(time());
$arr['date']=time();
insert($cfg['e'].'qr',$arr);
$err['code']=$arr['code'];
echo json_encode($err);
break;


case 'qr_is_login':
$code=g('code');
if(!$code){exit;}
$sql='select * from '.$cfg['e'].'qr where code=:code and user_id>0 ';
$p['code']=$code;
$l=db1($sql,$p);
if($l){
$time=$l['date']+600;
if($time<time()){err('抱歉，登录超时');}
$sql='select * from '.$cfg['e'].'users where id="'.$l['user_id'].'"';
$user=db1($sql);
if(!$user){err('没有找到用户信息');}
if(!$user['shop_id'] and !$user['is_emp']){err('抱歉，您不是门店管理员');}
$time=time()+3600*24*30;
setcookie($cfg['path'].'_user_id',$user['id'],$time,'/');
setcookie($cfg['path'].'_user_session',$user['session'],$time,'/');
err('ok');
}

break;


case 'help_edit'://帮助文档编辑
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$c=['title','c','info','tag','html','o','state'];
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);
if(!$data){err('数据错误');}

foreach($c as $v){
if($data[$v]){$arr[$v]=$data[$v];}
}

if(!$arr){err('没有找到需要输入的数据');}
if($arr['state']=='stop'){$arr['state']=0;}
if($id){
update($cfg['e'].'help',$arr,' where id="'.$id.'"');
}else{
insert($cfg['e'].'help',$arr);
}

err('ok');
break;
case 'help_del':
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
if(!$id){err('请指定需要删除的文档');}
$sql='delete from '.$cfg['e'].'help where id="'.$id.'"';
sql_query($sql);
err('ok');

break;

case 'change_apss'://修改密码
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);
$pass=$data['pass'];$old=$data['old'];
if(!$pass){err('请输入密码');}
if(!$old){err('请输入旧密码');}
$sql='select * from '.$cfg['e'].'adm where id="'.$adm['id'].'"';
$l=db1($sql);
if(!$l){err('数据错误，请联系管理员');}
if($l['password']!=md5($old)){err('旧密码输入错误');}
$arr['password']=md5($pass);
update($cfg['e'].'adm',$arr,' where id="'.$adm['id'].'"');
err('ok');
break;


case 'img_set_c':
$re=user_login();
if($re['err']!='ok'){err('登录错误');}else{
$user=$re['user'];
$shop=get_shop();
}
$data=p('data');
if(!$data){err('数据错误');}
$data=json_decode($data,1);
if(!$data){err('数据错误');}
$arr['path']=$data['path'];
$id=$data['id'];
if(!$arr['path']){err('请输入分类');}
if(!$id){err('请选择图片');}

$sql='where shop_id="'.$shop['id'].'" and id in('.$id.'0) ';
if(!$user['shop_id']){$sql.=' and user_id="'.$user['id'].'" ';}
update($cfg['e'].'img',$arr,$sql);
err('ok');


break;


case 'img_del'://删除图片
$re=user_login();
if($re['err']!='ok'){err('登录错误');}else{
$user=$re['user'];
$shop=get_shop();
}
$data=p('data');
if(!$data){err('数据错误');}
$data=json_decode($data,1);
$id=$data['id'];
if(!$id){err('请选择图片');}
$sql='select * from '.$cfg['e'].'img where shop_id="'.$shop['id'].'" and id in ('.$id.'0) ';
if(!$user['shop_id']){$sql.=' and user_id="'.$user['id'].'" ';}
$l=db($sql);
if($l){
foreach($l as $v){
if($v['url']){

}else{
$path='./image/'.ceil($v['id']/1000).'/';
$im=['240','480','750','1080'];
unlink($path.$v['id'].'.'.$v['ext']);
foreach($im as $t){
unlink($path.$v['id'].'_'.$t.'.'.$v['ext']);
}
}

$sql='delete from '.$cfg['e'].'img where id="'.$v['id'].'"';
sql_query($sql);
}


}
err('ok');

break;


case 'quick_creat_pro'://快速创建商品
$re=user_login();
if($re['err']!='ok'){err('登录错误');}else{
$user=$re['user'];
$shop=get_shop();
}
$data=p('data');
if(!$data){err('请选择商品图片');}
$data=json_decode($data,1);if(!$data['img']){err('请选择商品图片');}
$img=$data['img'];

//$arr['img']=$data['img'];


$arr['shop_id']=$shop['id'];
$arr['user_id']=$user['id'];
$arr['date']=time();
$arr['total']=-1;
$arr['title']='新商品';$arr['temp']=$arr['title'];
$arr['state']=0;
if($data['state']){$arr['state']=1;}
$arr['is_hide']=1;

$im=explode('||',$img);
foreach($im as $v){
if($v){
$arr['img']=$v;
$id=insert($cfg['e'].'pro',$arr);
}
}


$err['id']=$id;
echo json_encode($err);
break;




case 'pro_data_edit'://商品参数添加
    $re=user_login();
    if($re['err']!='ok'){err('登录错误');}else{
        $user=$re['user'];
        $shop=get_shop();
    }


    $id=g('id');if(!$id){err('请指定商品');}

    $sql='select * from '.$cfg['e'].'pro where id="'.a($id).'" and shop_id="'.$shop['id'].'" ';
    if($user['is_emp']){$sql.=' and user_id="'.a($user['id']).'" ';}

    $l=db1($sql);if(!$l){err('没有找到商品，或者您没有编辑的权限');}

    $data=p('data');if(!$data){err('数据错误');}
    $data=json_decode($data,1);if(!$data or !is_array($data)){err('数据错误');}
    $k=g('k');if(!$k){$k=0;}

    $c=g('c');if($c!='edit'){$c='add';}
    if($l['data']){$d=unserialize(out($l['data']));}else{$d=[];}
    if(!$data['c']){err('请输入分类');}
    if($c=='add'){
        if($data['num']!=-1){if($data['num']<-1){$data['num']=0;}}
        if($d[$data['c']]){//添加
//print_r($data);
            if(!$data['c']){err('请输入标题');}
            if(!$data['price']){err('请输入商品的价格');}

            $d[$data['c']]['item'][]=array('title'=>$data['c'],'price'=>$data['price'],'num'=>$data['num']);
        }else{//新增分类
            $d[$data['c']]=array('price'=>$data['price'],'num'=>$data['num'],'title'=>$data['c'],'xiangou'=>$data['xiangou']);
        }

    }else{//编辑
        if(!$data['c']){err('分类不能为空');}
        foreach($data as $m=>$n){
            if($m!='c'){
                if(!$d[$data['c']]['item'] or $k=='main'){
                    $d[$data['c']][$m]=$n;
                }else{
                    $d[$data['c']]['item'][$k][$m]=$n;}
            }
        }
    }

    $num=pro_num($d);
    if($num==0 and $l['total']!=-1){$arr['total']=0;}else{
        $arr['total']=$num;
    }

    $arr['data']=serialize($d);
    update($cfg['e'].'pro',$arr,' where id="'.$l['id'].'"');
    err('ok');
    break;








case 'pro_change':
$re=user_login();
if($re['err']!='ok'){err('登录错误');}else{
$user=$re['user'];
$shop=get_shop();
}

if(!$id){err('请指定商品');}
$sql='select * from '.$cfg['e'].'pro where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if(!$user['shop_id']){$sql.=' and user_id="'.$user['id'].'" ';}
$l=db1($sql);
if(!$l){err('没有找到商品，或者您没有编辑的权限');}
$data=p('data');if(!$data){err('参数错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
$c=['title','state','price','old','c','html','o','img','total','is_new','is_hot','is_hide','daiwei','jt_price','info',"data"];
foreach($c as $v){
if($data[$v]){$arr[$v]=$data[$v];}
}
if(!$arr['state']){
    if($arr['data']){
        $d=[];
        $k = 0;
        foreach($data['data'] as $val){

            if($val['num']!=-1){if($val['num']<-1){$val['num']=0;}}
            if($d[$val['c']]){//添加
                if(!$val['c']){err('请输入标题');}
                if(!$val['price']){err('请输入商品的价格');}

                $d[$val['c']]['item'][]=array('title'=>$val['c'],'price'=>$val['price'],'num'=>$val['num']);
            }else{//新增分类
                $d[$val['c']]=array('price'=>$val['price'],'num'=>$val['num'],'title'=>$val['c'],'xiangou'=>$val['xiangou']);
            }
        }
        $num=pro_num($d);
        if($num==0 and $l['total']!=-1){$arr['total']=0;}else{
            $arr['total']=$num;
        }

        $arr['data']=serialize($d);
    }else{
        $arr['data']= null;
    }
};
if($arr['title']){
    $arr['temp']=$arr['title'];
    $arr['title']=base64_encode($arr['title']);
    
}
if($arr['state']){
if($arr['state']=='stop'){$arr['state']=0;}else{$arr['state']=1;}
}

if($arr['is_new']){
if($arr['is_new']=='stop'){$arr['is_new']=0;}else{$arr['is_new']=1;}
}
if($arr['is_hot']){
if($arr['is_hot']=='stop'){$arr['is_hot']=0;}else{$arr['is_hot']=1;}
}
if($arr['is_hide']){
if($arr['is_hide']=='stop'){$arr['is_hide']=0;}else{$arr['is_hide']=1;}
}

if($arr['total'] and !is_numeric($arr['total'])){$arr['total']=0;}
if($arr['total']<0){$arr['total']=-1;}

if($arr['price'] and !is_numeric($arr['price'])){$arr['price']=0;}
if($arr['old'] and !is_numeric($arr['old'])){$arr['old']=0;}

if($arr['price']>0 and $arr['old']>0){
if($arr['old']<=$arr['price']){err('划线价不能小于商品售价');}
}



if($arr['html']){
$arr['html']=base64_encode(serialize($arr['html']));
}
if($arr['jt_price']){
$arr['jt_price']=serialize($arr['jt_price']);
}

if(!$arr){err('保存数据错误');}
update($cfg['e'].'pro',$arr,' where id="'.$l['id'].'"');
err('ok');
break;


case 'pro_del':
$re=user_login();
if($re['err']!='ok'){err('登录错误');}else{
$user=$re['user'];
$shop=get_shop();
}

if(!$id){err('请指定商品');}
$sql='select * from '.$cfg['e'].'pro where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if(!$user['shop_id']){$sql.=' and user_id="'.$user['id'].'" ';}
$l=db1($sql);
if(!$l){err('没有找到商品，或者您没有删除的权限');}
$sql='delete from '.$cfg['e'].'pro where id="'.$l['id'].'"';
sql_query($sql);
err('ok');
break;



case 'hd_state':
$re=user_login();
if($re['err']!='ok'){err('登录错误');}else{
$user=$re['user'];
$shop=get_shop();
}
if(!$id){err('请指定活动');}
$sql='select * from '.$cfg['e'].'hd where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if(!$user['shop_id']){$sql.=' and user_id="'.$user['id'].'" ';}
$l=db1($sql,$p);if(!$l){err('没有找到活动信息，或者您没有编辑的权限');}
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
$arr['state']=$data['state'];

if($arr['state']==1){
if(!$l['pro']){err('抱歉，您的活动中需要一些商品');}
//if($l['end']<time()){err('抱歉，您的活动已经结束了，请修改结束时候后继续');}
if(!$l['title']){err('您没有编辑活动的标题，请输入一个优质的标题后继续');}
if(!$l['yun_id']){err('请选择一款合适的运费模板');}
$pro=unserialize(out($l['pro']));//活动商品
$pros=get_pros($pro,$l['zk']);
//对商品进行分析
$min=0;$max=0;
foreach($pros as $v){
if(!$v['title']){err('有部分商品标题为空');}
if(!$v['price']){err('抱歉，您的部分商品价格为0');}
if($v['total']==0){$err['err_msg']='抱歉，您的部分商品库存设置为0';}
if(!$min){$min=$v['price'];}
if($v['price']<$min){$min=$v['price'];}
if($v['price']>$max){$max=$v['price'];}

}

$arr['min']=$min;
if($l['zk']<100){$min=number_format($min*$l['zk']/100,2,'.','');$max=number_format($max*$l['zk']/100,2,'.','');}
$arr['max']=$max;

//秒杀
if($l['miao_price'] and $l['miao_pro']){
if($l['miao_price']<$min){$min=$l['miao_price'];}
}



if(!$l['img']){err('请选择至少一张海报图片。');}

if($shop){
$arr['shop_name']=base64_encode($shop['shop_name']);
$arr['shop_addr']=$shop['addr'];
$arr['logo']=$shop['logo'];
if($shop['latitude']){
$arr['latitude']=$shop['latitude'];
$arr['longitude']=$shop['longitude'];
}
}

//如果有微信红包
if($l['hb_price'] and $l['hb_num'] and $l['hb_total'] ){
$hb_num=$l['hb_total']-$l['hb_is_get'];
$hb_price=number_format($l['hb_price']*$hb_num,2,'.','');
//冻结资金
if($shop['balance']<$hb_price){err('抱歉，您的现金红包所需冻结资金不够，请充值或调整红包金额数量');}
//账单
$zd['shop_id']=$shop['id'];
$zd['date']=time();
$zd['hd_id']=$l['id'];
$zd['nickname']=$user['nickname'];
$zd['logo']=$user['logo'];
//管理员账单
$zd['user_id']=$user['id'];
$zd['title']='现金红包冻结资金';
$zd['c']='资金冻结';
$zd['money']=-$hb_price;//金额
insert($cfg['e'].'pay',$zd);
zd_balance($shop);


$sql='update '.$cfg['e'].'shop set freeze=freeze+'.$hb_price.' where id="'.$l['shop_id'].'"';
sql_query($sql);

}


$arr['state']=1;
update($cfg['e'].'hd',$arr,'  where id="'.$l['id'].'"');

//增加1
$sql='update '.$cfg['e'].'shop set hd_num=hd_num+1 where id="'.$l['shop_id'].'"';
sql_query($sql);
$shop_arr['hd_bg']=$l['bg'];
$shop_arr['yun_id']=$l['yun_id'];
$shop_arr['yun_title']=$l['yun_title'];
update($cfg['e'].'shop',$shop_arr,' where id="'.$l['shop_id'].'"');

if(!$err['err_msg']){
$err['err_msg']='您的活动已经成功开启，您可以转发活动到朋友圈或者公众号。';}

//创建活动消息模板
get_activity_id($l);

set_share_img($l);//设置分享图
pm_add(1,$shop,'活动上线');

echo json_encode($err);exit;

}else{//活动关闭
$arr['state']=0;
update($cfg['e'].'hd',$arr,'  where id="'.$l['id'].'"');
//建1
$sql='update '.$cfg['e'].'shop set hd_num=hd_num-1 where id="'.$l['shop_id'].'"';
sql_query($sql);



err('ok');
}

break;

case 'hd_del':
$re=user_login();
if($re['err']!='ok'){err('登录错误');}else{
$user=$re['user'];
$shop=get_shop();
}
if(!$id){err('请指定活动');}
$sql='select * from '.$cfg['e'].'hd where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}
$l=db1($sql);
if(!$l){err('没有找到活动，或者您没有管理的权限');}
if($l['order_num']>0){err('抱歉，您的活动已经存在订单，无法删除该活动');}
if($l['state']!=0){err('请停止活动后继续');}
$sql='delete from '.$cfg['e'].'hd where id="'.$l['id'].'"';
sql_query($sql);
err('ok');
break;


case 'hd_save':
$re=user_login();
if($re['err']!='ok'){err('登录错误');}else{
$user=$re['user'];
$shop=get_shop();
}
if(!$id){err('请指定活动');}
$sql='select * from '.$cfg['e'].'hd where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if(!$user['shop_id']){$sql.=' and user_id="'.$user['id'].'" ';}
$l=db1($sql,$p);if(!$l){err('没有找到活动信息，或者您没有编辑的权限');}
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
$c=['title','info','html','start','end','img','bg','pro','tag','miao_price','miao_pro','miao_title','mz_price','mz_pro','mz_title','yun_id','yun_title','show_name','zk','miao_total','mz_total','warn','nuo_data','fen_data','virtual_people'];

foreach($c as $v){
if($data[$v] or is_numeric($data[$v])){$arr[$v]=$data[$v];}
}

if($arr['title']){$arr['temp']=$arr['title'];$arr['title']=base64_encode($arr['title']);}
if($arr['info']){$arr['info']=base64_encode($arr['info']);}
if($arr['html']){$arr['html']=base64_encode(serialize($arr['html']));}
if($arr['tag']){if($arr['tag']=='none'){$arr['tag']='';}else{$arr['tag']=serialize($arr['tag']);}}
if($arr['fen_data']){if($arr['fen_data']=='clean'){$arr['fen_data']='';}}
if($arr['nuo_data']){if($arr['nuo_data']=='clean'){$arr['nuo_data']='';}}
if($arr['miao_pro']=='clean'){$arr['miao_pro']=0;}
if($arr['mz_pro']=='clean'){$arr['mz_pro']=0;}
if($arr['start']){$arr['start']=get_time($arr['start']);}
if($arr['end']){$arr['end']=get_time($arr['end']);}

update($cfg['e'].'hd',$arr,' where id="'.$l['id'].'"');
err('ok');
break;



case 'hd_pro_ac':
$re=user_login();
if($re['err']!='ok'){err('登录错误');}else{
$user=$re['user'];
$shop=get_shop();
}
if(!$id){err('请指定活动');}
$sql='select * from '.$cfg['e'].'hd where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if(!$user['shop_id']){$sql.=' and user_id="'.$user['id'].'" ';}
$l=db1($sql,$p);if(!$l){err('没有找到活动信息，或者您没有编辑的权限');}
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
if($l['pro']){$pro=unserialize(out($l['pro']));}
if(!$data['id']){err('没有选择商品');}
$id=$data['id'];
switch($data['ac']){
case 'del':
unset($pro['item_'.$id]);
break;
case 'up':
$temp['item_'.$id]=$pro['item_'.$id];
unset($pro['item_'.$id]);
if($pro){
foreach($pro as $k=>$v){
$temp[$k]=$v;
}
}
$pro=$temp;
break;
case 'add':
$pro['item_'.$id]=array('id'=>$id,'sell'=>0);
break;
}

if($pro){
$arr['pro']=serialize($pro);
}else{
$arr['pro']='';
}
update($cfg['e'].'hd',$arr,' where id="'.$l['id'].'"');
err('ok');
break;



case 'app_view'://浏览结果反馈
$user=is_login();
if($user){
$arr['user_id']=$user['id'];
$arr['logo']=$user['logo'];
$arr['nickname']=base64_encode($user['nickname']);
}
$arr['session']=g('user_session');
$arr['date']=time();
$data=p('data');
if(!$data){exit;}
$data=json_decode($data,1);
$arr['data']=serialize($data);
insert($cfg['e'].'view',$arr);
err('ok');
break;


case 'emp_state'://雇员状态
$re=user_login();
if($re['err']!='ok'){err('登录错误');}
$user=$re['user'];
$shop=get_shop();
if(!$user['shop_id']){err('抱歉，仅门店管理有权限');}
if(!$id){err('请指定雇员');}
$data=p('data');if(!$data){err('参数错误');}
$data=json_decode($data,1);if(!$data){err('参数错误');}
$arr['emp_state']=$data['state'];
if($arr['emp_state']!=1){$arr['emp_state']=0;}
update($cfg['e'].'users',$arr,' where id="'.$id.'" and is_emp="'.$shop['id'].'"');
err('ok');
break;


case 'get_temps'://导入消息模板
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
$token=getAccessToken($cfg['wx_xcx_appid'], $cfg['wx_xcx_secret']); 
$url='https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token='.$token;
$re=file_get_contents($url);
if($re){
$data=json_decode($re,1);
if($data['errcode']==0){
$sql='select * from '.$cfg['e'].'temps where c="user" order by id desc';   
$l=db($sql);
if($l){
foreach($l as $v){
$ll[$v['temp_id']]=$v;
}
}

foreach($data['data'] as $v){
$arr['temp_id']=$v['priTmplId'];
$arr['title']=$v['title'];
$arr['data']=$v['content'];
$arr['c']='user';

if(!$ll[$arr['temp_id']]){
insert($cfg['e'].'temps',$arr);
$msg.=$arr['title'].'添加成功<br>';
}else{
$msg.=$arr['title'].'已经存在<br>';
}

}
}
}
err($msg);
break;

case 'temp_edit':
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
$data=p('data');if(!$data){err('数据错误');}
$arr=json_decode($data,1);if(!$arr){err('数据错误');}
if($id){
update($cfg['e'].'temps',$arr,' where id="'.$id.'"');
}else{
insert($cfg['e'].'temps',$arr);
}
err('ok');
break;
case 'get_temp'://获取单个模板
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
if(!$id){err('请指定模板');}
$sql='select * from '.$cfg['e'].'temps where id="'.$id.'" and c="temp"';
$l=db1($sql);if(!$l){err('抱歉，没有找到模板');}
if(!$l['data']){err('该模板缺少参数');}
if(!$l['temp_id']){err('模板ID缺少');}
$sql='select * from '.$cfg['e'].'temps where from_id="'.$id.'"';
$ck=db1($sql);if($ck){err('该模板已经存在，如果需要更新，请删除后重新获取 ');}


$token=getAccessToken($cfg['wx_xcx_appid'], $cfg['wx_xcx_secret']); 
$url='https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token='.$token;
$l['data']=str_replace(array(['[',']','，',',']),array('','',',',','),$l['data']);
$d=explode(',',$l['data']);
foreach($d as $v){
if($v){
$dd[]=$v;
}
}


//$data=json_encode($data);
$data='{
  "tid":"'.$l['temp_id'].'",  
    "kidList":['.implode(',',$dd).'],
    "sceneDesc":"api获取"
}';
//print_r($data);
$re=send_post($url,$data);
$re=json_decode($re,1);
//print_r($re);
if($re['errmsg']=='ok'){
$arr['c']='user';
$arr['from_id']=$l['id'];
$arr['temp_id']=$re['priTmplId'];
$arr['title']=$l['title'];
$arr['s']=$l['s'];

insert($cfg['e'].'temp',$arr);
err('ok');
}else{
err($re['errmsg']);
}

break;

case 'temp_del':
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
if(!$id){err('请指定模板');}
$sql='delete from  '.$cfg['e'].'temps where id="'.$id.'"';
sql_query($sql);
err('ok');
break;

case 'emoji_add':
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
$data=p('data');if(!$data){err('请输入数据');}
$data=json_decode($data,1);if(!$data){err('请输入数据');}
$arr['emoji']=$data['emoji'];
insert($cfg['e'].'emoji',$arr);
err('ok');

break;
case 'emoji_up':
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];if(!$id){err('请选择图标');}
$arr['o']=time();
update($cfg['e'].'emoji',$arr,' where id="'.$id.'"');err('ok');
break;

case 'emoji_del':
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];if(!$id){err('请选择图标');}
$sql='delete from '.$cfg['e'].'emoji where id="'.$id.'"';
sql_query($sql);
err('ok');
break;




case 'shop_edit'://门店信息编辑
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
$data=p('data');if(!$data){err('参数错误');}
$arr=json_decode($data,1);
if($arr['shop_name']){$arr['shop_name']=base64($arr['shop_name'],1);}
update($cfg['e'].'shop',$arr,' where id="'.$id.'"');
err('ok');
break;

case 'shop_adm_edit':
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
$data=p('data');if(!$data){err('参数错误');}
$arr=json_decode($data,1);
update($cfg['e'].'users',$arr,' where id="'.$id.'"');
err('ok');

break;

case 'adm_user_change':
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
$data=p('data');if(!$data){err('参数错误');}
$arr=json_decode($data,1);
update($cfg['e'].'users',$arr,' where id="'.$id.'"');
err('ok');
break;

case 'ts_jy_submit':
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];
$data=p('data');if(!$data){err('参数错误');}
$arr=json_decode($data,1);
update($cfg['e'].'ts',$arr,' where id="'.$id.'"');
err('ok');
break;


case 'get_qr_data':
if(!$id){err('请指定活动');}
$page='pages/index/detail';
$data=p('data');
if($data){$data=json_decode($data,1);}
if($data['page']){$page=$data['page'];

}

$d=array('scene'=>'id='.$id.'&f=','width'=>400,'is_hyaline'=>true,'page'=>$page,'check_path'=>false);
$path='./cache/';if(!file_exists($path)){mkdir($path);}
$path='./cache/qr/';if(!file_exists($path)){mkdir($path);}
if($data['page']){
$file=str_replace('/','',$page);
}else{
$file='hd_'.$id.'_u_0.png';
}
if(!file_exists($path.$file)){
$qr_file=$path.$file;
$qr=get_qr($d);
if(strlen($qr)>100){}else{err('二维码生成失败');}
$newFile = fopen($qr_file,"w"); //打开文件准备写入
fwrite($newFile,$qr); //写入二进制流到文件
fclose($newFile); //关闭文件
}

$err['qr']=$server.'cache/qr/'.$file;
echo json_encode($err);
break;

case 'adm_add':
$re=adm_login();
if($re['err']=='no_login'){err('no_login');}
if($re['err']!='ok'){err($re['err']);}
$adm=$re['adm'];if($adm['power']!='adm'){err('权限错误');}
$data=p('data');if(!$data){err('参数错误');}
$arr=json_decode($data,1);
if(!$arr){err('参数错误');}
$arr['date']=time();
$arr['state']=1;
$arr['password']=md5($arr['pass']);
unset($arr['pass']);
$sql='select * from '.$cfg['e'].'adm where username="'.$arr['username'].'"';
$l=db1($sql);
if($l){err('抱歉，该登录名已经存在');}


insert($cfg['e'].'adm',$arr);
err('ok');
break;

case 'printer'://打印订单
    $re=user_login();
    if($re['err']!='ok'){err('登录错误');}else{
        $user=$re['user'];
        $shop=get_shop();
    }
    $data=p('data');if(!$data){err('数据错误');}
    $data=json_decode($data,1);if(!$data){err('数据错误');}
    $order_id=$data['order_id'];if(!$order_id){err('请选择订单');}
    err( printer($order_id));
    break;

    case 'print_save':
        $re=user_login();
        if($re['err']!='ok'){err('登录错误');}else{
            $user=$re['user'];
            $shop=get_shop();
        }
        $data=p('data');if(!$data){err('数据错误');}
        $data=json_decode($data,1);if(!$data){err('数据错误');}

        $shop_id=$data["data"]["shop_id"];//查找门店
        if(!$shop_id){err('请指定门店');}

        $sql='select * from '.$cfg['e'].'shop ';
        $p['id']=$shop_id;
        $l=db1($sql,$p);
        if(!$l){err('没有找到门店或您没有保存的权限');}

        if($id){
            $arr=$data['data'];
            update($cfg['e'].'print',$arr,' where id="'.$id.'"');
            err('ok');
        }else{
            $arr['name']=$data["data"]['name'];if(!$arr['name']){err('请输入打印机名称');}
            $arr['appid']=$data["data"]['appid'];if(!$arr['appid']){err('请输入打印机终端号');}
            $arr['appkey']=$data["data"]['appkey'];if(!$arr['appkey']){err('请输入打印机密匙');}
            $arr['adm_id']='0';
            $arr['shop_id']=$shop_id;
            $arr['date']=time();
            $arr['c']='易联云';$arr['auto']=1;
//链接打印机
            $token=get_print_token();
            if(!$token){err('系统错误，请联系我们处理');}
            unset($data["data"]);
            $data["data"]['client_id']=$cfg['print_id'];
            $data["data"]['machine_code']=$arr['appid'];
            $data["data"]['msign']=$arr['appkey'];
            $data["data"]['access_token']=$token['token'];
            $data["data"]['sign']=$token['sign'];
            $data["data"]['id']=$token['uuid'];
            $data["data"]['timestamp']=$token['timestamp'];
            $data["data"]['print_name']=$arr['name'];
            $url='https://open-api.10ss.net/printer/addprinter';
            $HTTP_headers='Content-Type:application/x-www-form-urlencoded';
            $re=curlPost($url,$data["data"],$HTTP_headers);
            $re=json_decode($re,1);
            if($re['error']==0){
                insert($cfg['e'].'print',$arr);
                err('ok');
            }else{
                err($re['error_description']);
            }
        }


        break;
    case 'print_add'://添加打印机
        $re=user_login();
        if($re['err']!='ok'){err('登录错误');}else{
            $user=$re['user'];
            $shop=get_shop();
        }
        $data=p('data');if(!$data){err('数据错误');}
        $data=json_decode($data,1);if(!$data){err('数据错误');}

        $shop_id=$data["data"]["shop_id"];//查找门店
        if(!$shop_id){err('请指定门店');}

        $arr['name']=$data["data"]['name'];if(!$arr['name']){err('请输入打印机名称');}
        $arr['appid']=$data["data"]['appid'];if(!$arr['appid']){err('请输入打印机终端号');}
        $arr['appkey']=$data["data"]['appkey'];if(!$arr['appkey']){err('请输入打印机密匙');}
        $arr['adm_id']='0';
        $arr['shop_id']=$shop_id;
        $arr['date']=time();
        $arr['c']='易联云';$arr['auto']=1;
//链接打印机
        $token=get_print_token();
        if(!$token){err('系统错误，请联系我们处理');}
        unset($data["data"]);
        $data["data"]['client_id']=$cfg['print_id'];
        $data["data"]['machine_code']=$arr['appid'];
        $data["data"]['msign']=$arr['appkey'];
        $data["data"]['access_token']=$token['token'];
        $data["data"]['sign']=$token['sign'];
        $data["data"]['id']=$token['uuid'];
        $data["data"]['timestamp']=$token['timestamp'];
        $data["data"]['print_name']=$arr['name'];
        $url='https://open-api.10ss.net/printer/addprinter';
        $HTTP_headers='Content-Type:application/x-www-form-urlencoded';
        $re=curlPost($url,$data["data"],$HTTP_headers);
        $re=json_decode($re,1);

        if($id){
            update($cfg['e'].'print',$arr,' where id="'.$id.'"');
            err('ok');
        }else{
            if($re['error']==0){
                insert($cfg['e'].'print',$arr);
                err('ok');
            }else{
                err($re['error_description']);
            }
        }
        break;

case 'export'://导出订单
    $re=user_login();
    if($re['err']!='ok'){err('登录错误');}else{
        $user=$re['user'];
        $shop=get_shop();
    }
    $data=p('data');if(!$data){err('数据错误');}
    $data=json_decode($data,1);if(!$data){err('数据错误');}
    exportExcel($data);
    break;



}













?>